const jwt = require('jsonwebtoken');
const whiteList = ['/login','/registry']; //白名单，不需要校验的放在白名单里面
module.exports = ()=>{
    return async (ctx,next)=>{
        if(whiteList.includes(ctx.path)){ //在里面 
            await next();
        } else {
            let token = ctx.request.headers.authorization;
            if(!token){
                ctx.body = {
                    code:5,
                    mes:'没有权限'
                }
                return;
            }
            try{
                let userInfo = jwt.verify(token,ctx.app.config.keys);
                console.log(userInfo);
                await next();
            } catch(e){
               if (e.name=="TokenExpiredError") {
                   ctx.body={
                       code:1,
                       mes:"token过期了"
                   }
               }
            }
        }
    }
}